package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.RoundScore;
import org.example.rowing.domain.Team;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RegistrationVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CKProcessTenToEighteenBoatsService implements RaceProcessStrategy {

    @Autowired
    private RaceInfoService raceInfoService;
    @Autowired
    private RaceProcessCommonService raceProcessCommonService;

    public static final List<String> teamLanes = Arrays.asList("",
            "A组5号", "A组4号", "A组6号", "A组3号", "A组7号", "A组2号", "A组8号", "A组1号", "A组9号",
            "B组5号", "B组4号", "B组6号", "B组3号", "B组7号", "B组2号", "B组8号", "B组1号", "B组9号"
    );

    /**
     * 获取预赛晋级决赛的列表
     * @param curList
     * @return
     */
    public void getAdvancingToFinals(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> advancingToSemis = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);

        for (int i = 0; i < 3; i++) {
            advancingToSemis.add(groupAList.get(i));
            advancingToSemis.add(groupBList.get(i));
        }

        raceProcessCommonService.sortedEntries(advancingToSemis);

        for (int i = 0; i < advancingToSemis.size(); i++) {
            RegistrationVO registrationVO = advancingToSemis.get(i).getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 1), null);
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取预赛晋级半决赛的列表
     * @param curList
     * @param raceId
     * @return
     */
    public void getAdvancingToHalfFinals(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> advancingToSemis = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);

        int t1 = Math.min(groupAList.size(), 7);
        for (int i = 3; i < t1; i++) {
            advancingToSemis.add(groupAList.get(i));
        }
        int t2 = Math.min(groupBList.size(), 7);
        for (int i = 3; i < t2; i++) {
            advancingToSemis.add(groupBList.get(i));
        }
        if (groupAList.size() >= 7 && groupBList.size() >= 7) {
            if (Double.compare(groupAList.get(6).getScore(), groupBList.get(6).getScore()) > 0) {
                advancingToSemis.add(groupBList.get(6));
            } else {
                advancingToSemis.add(groupAList.get(6));
            }
        } else if (groupAList.size() >= 7) {
            advancingToSemis.add(groupAList.get(6));
        } else if (groupBList.size() >= 7) {
            advancingToSemis.add(groupBList.get(6));
        }

        raceProcessCommonService.sortedEntries(advancingToSemis);

        for (int i = 0; i < advancingToSemis.size(); i++) {
            RegistrationVO registrationVO = advancingToSemis.get(i).getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 1), null);
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取半决赛晋级决赛的列表
     * @param curList
     * @param raceId
     * @return
     */
    public void getHalfFinalsToFinals(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> advancingToSemis = new ArrayList<>();

        raceProcessCommonService.sortedEntries(curList);

        for (int i = 0; i < 3; i++) {
            advancingToSemis.add(curList.get(i));
        }

        for (int i = 0; i < advancingToSemis.size(); i++) {
            RegistrationVO registrationVO = advancingToSemis.get(i).getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 7), null);
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取最终排名
     * @param curList
     * @param raceVO
     * @return
     */
    public List<RoundScoreVO> getRank(List<RoundScoreVO> curList, RaceVO raceVO) {
        raceProcessCommonService.sortedEntries(curList);

        saveFinalResult(curList, raceVO);
        int i = 0;
        for (RoundScoreVO roundScoreVO : curList) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return curList;
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
